#include "widget.h"
#include "ui_widget.h"
#include<QDebug>
#include<QRegExpValidator>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,&Widget::handle);
    timer->start(100);
    ui->pushButton_summit->setEnabled(false);
    ui->lineEdit_name->setPlaceholderText("请输入姓名");
    ui->lineEdit_name->setClearButtonEnabled(true);

    ui->lineEdit_password->setPlaceholderText("请输入密码");
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);

    ui->lineEdit_phone->setPlaceholderText("请输入电话号码");
    ui->lineEdit_phone->setClearButtonEnabled(true);
    QRegExp regexp("^1\\d{10}$");
    ui->lineEdit_phone->setValidator(new QRegExpValidator(regexp));

    ui->lineEdit->setEchoMode(QLineEdit::Password);
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handle()
{
    int value=ui->progressBar->value();
    if(value>100)
    {
        timer->stop();
        return ;
    }
    ui->progressBar->setValue(value+1);
}


void Widget::on_calendarWidget_selectionChanged()
{
    QDate date=ui->calendarWidget->selectedDate();
    qDebug()<<date;
    ui->label->setText(date.toString());
}

void Widget::on_pushButton_summit_clicked()
{
    QString gender=ui->radioButton_male->isChecked()? "男":"女";
    qDebug()<<"姓名: "<<ui->lineEdit_name->text()
            <<"密码: "<<ui->lineEdit_password->text()
            <<"性别: "<<gender
            <<"电话: "<<ui->lineEdit_phone->text();
}

void Widget::on_lineEdit_phone_textEdited(const QString &text)
{
    QString content=text;
    int pos=0;
    if(ui->lineEdit_phone->validator()->validate(content,pos)==QValidator::Acceptable)
    {
        ui->pushButton_summit->setEnabled(true);
    }
    else
    {
        ui->pushButton_summit->setEnabled(false);
    }
}

void Widget::on_lineEdit_2_textEdited(const QString &arg1)
{
    (void) arg1;
    this->compare();
}

void Widget::on_lineEdit_textEdited(const QString &arg1)
{
    (void) arg1;
   this->compare();
}

void Widget::compare()
{
    const QString s1=ui->lineEdit->text();
    const QString s2=ui->lineEdit_2->text();
    if(s1.isEmpty()&&s2.isEmpty())
    {
        ui->label_6->setText("密码为空");
    }
    else if(s1==s2)
    {
        ui->label_6->setText("两次密码一致");
    }
    else
    {
        ui->label_6->setText("两次密码不一致,请重新输入");
    }
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit->setEchoMode(QLineEdit::Normal);
        ui->lineEdit_2->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit->setEchoMode(QLineEdit::Password);
        ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    }
}

void Widget::on_textEdit_textChanged()
{
    const QString& text=ui->textEdit->toPlainText();
    ui->label_7->setText(text);
}

void Widget::on_textEdit_selectionChanged()
{
    QTextCursor cursor=ui->textEdit->textCursor();
    qDebug()<<cursor.selectedText();
}

void Widget::on_textEdit_cursorPositionChanged()
{
    QTextCursor cursor=ui->textEdit->textCursor();
    qDebug()<<cursor.position();
}
